﻿#include "dialog.h"
#include "ui_dialog.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include "StageControl/StageControlB140/dmc_b140_m.h"
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);

    initGUI();

    m_pMotionControl = new DMC_B140_M(this);


    connect(m_pMotionControl, &DMC_B140_M::connectionSetup, this, &Dialog::onConnected);
    connect(m_pMotionControl, &DMC_B140_M::connectionLost, this, &Dialog::onConnectionLost);



    connect(m_pAxisWidget, &AxisWidget::do_jog, m_pMotionControl, &MotionController::move);
    connect(m_pAxisWidget, &AxisWidget::do_home, m_pMotionControl, &MotionController::home);
    connect(m_pAxisWidget, &AxisWidget::do_stop, m_pMotionControl, &MotionController::stopAxis);
    connect(m_pAxisWidget, &AxisWidget::do_zero, m_pMotionControl, &MotionController::setTheoryPos);
    connect(m_pAxisWidget, &AxisWidget::do_zero, m_pMotionControl, &MotionController::setEncoderPos);
    connect(m_pMotionControl, &MotionController::positionChanged, m_pAxisWidget, &AxisWidget::setPos);
    connect(m_pMotionControl, &MotionController::limitStatusChanged, m_pAxisWidget, &AxisWidget::setLimitStatus);



    connect(m_pAxisWidget, &AxisWidget::speedChanged, m_pMotionControl, &MotionController::setSpeed);
    connect(m_pAxisWidget, &AxisWidget::accChanged, m_pMotionControl, &MotionController::setAcceleration);
    connect(m_pAxisWidget, &AxisWidget::decChanged, m_pMotionControl, &MotionController::setDeceleration);

    connect(m_pIOWidget, &IOWidget::outPortChanged, m_pMotionControl, &MotionController::writeIOBit);
    connect(m_pMotionControl, &DMC_B140_M::INPortChanged, m_pIOWidget, &IOWidget::setINPort);

//    m_timerXYZPos = new QTimer(this);
//    m_timerXYZPos->setInterval(100);
//    connect(m_timerXYZPos, SIGNAL(timeout()), this, SLOT(updatePos()));
//    connect(m_timerXYZPos, SIGNAL(timeout()), this, SLOT(updateIO()));
//    m_timerXYZPos->start();
}

Dialog::~Dialog()
{
    delete m_pAxisWidget;
    delete m_pIOWidget;
    delete m_pMotionControl;

    delete ui;
}

void Dialog::initGUI()
{
    m_pLineEditIp = new QLineEdit(this);
    m_pLineEditIp->setText("192.168.2.140");

    m_pPushButtonConnect = new QPushButton(this);
    m_pPushButtonConnect->setText(u8"连接");

    QLabel * l = new QLabel(this);
    l->setText(u8"DMC B140M 地址：");

    m_pPushButtonAbort = new QPushButton;
    m_pPushButtonAbort->setText(u8"急停");

    QHBoxLayout *pHBoxLayout = new QHBoxLayout;
    pHBoxLayout->addWidget(l);
    pHBoxLayout->addWidget(m_pLineEditIp);
    pHBoxLayout->addWidget(m_pPushButtonConnect);
    pHBoxLayout->addStretch();
    pHBoxLayout->addWidget(m_pPushButtonAbort);


    m_pAxisWidget = new AxisWidget(this);
    m_pIOWidget = new IOWidget(this);

    QVBoxLayout *pVBoxLayout = new QVBoxLayout;
    pVBoxLayout->addLayout(pHBoxLayout);
    pVBoxLayout->addWidget(m_pAxisWidget);
    pVBoxLayout->addWidget(m_pIOWidget);

    setLayout(pVBoxLayout);


    connect(m_pPushButtonConnect, &QPushButton::clicked, this, &Dialog::onConnect);
}


void Dialog::onConnect()
{
    QString ip = m_pLineEditIp->text();
    DMC_B140_M * p = dynamic_cast<DMC_B140_M *>(m_pMotionControl) ;
    p->connectViaEthernet(ip, 29078);
}

void Dialog::onConnected()
{
    QMessageBox::information(this, "DMC B140M", u8"连接成功！");
}

void Dialog::onConnectionLost()
{
    QMessageBox::information(this, "DMC B140M", u8"连接断开！");
}

void Dialog::updateAlarmStatus(unsigned short axis, bool alarm, bool emstop)
{

}

